package com.testng;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * @author wq
 * @since 2022-06-15 10:56
 */

public class TestngDemo {

    public static void main(String[] args) throws Exception {
        int startRow = 2;
        int endRow = 5;
        int[] columns = {1,3,4};
        Object[][] datas = getDatas("src/api_testcases_futureloan_v1.xls", 1, startRow, endRow, columns);
        System.out.println(datas);
    }
//    public Object[][] datas(){
//
//        return null;
//    }

    /**
     *
     * @param excelPath 用例文件路径
     * @param sheetNum  表单序号，取哪一页
     * @param startRow  开始的行
     * @param endRow   结束的行
     * @param columns  获取所有列组成的数据
     * @return
     */
    public static Object[][] getDatas(String excelPath,int sheetNum,int startRow,int endRow,int[] columns) throws Exception {
        //准备数组要保存的测试数据
        Object[][] datas = new Object[endRow - startRow + 1][columns.length];
        InputStream inputStream = null;
        //获取输入流对象
        inputStream = new FileInputStream(excelPath);
        //创建工作簿
        Workbook workbook = WorkbookFactory.create(inputStream);
        //获取表单页数
        Sheet sheet = workbook.getSheetAt(sheetNum - 1);
        //循环获取所有的行
        for (int i = startRow; i < endRow; i++) {
            Row row = sheet.getRow(i - 1);
            //循环遍历列
            for (int j = 0; j < columns.length; j++) {
                Cell cell = row.getCell(columns[j]);
                cell.setCellType(CellType.STRING);
                String cellValue = cell.getStringCellValue();
                datas[i - startRow][j] = cellValue;
            }
        }
        return datas;
    }
}
